package de.grosjohann.android.feuerwehr;

import java.util.List;

import org.json.JSONException;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.BaseAdapter;
import android.widget.ListView;
import de.grosjohann.android.feuerwehr.adapter.CalendarListAdapter;
import de.grosjohann.android.feuerwehr.domain.CalendarItem;
import de.grosjohann.android.feuerwehr.notification.Notify;
import de.grosjohann.android.feuerwehr.panel.Panel;
import de.grosjohann.android.feuerwehr.service.CalendarDisplayService;
import de.grosjohann.android.feuerwehr.service.CalendarNotificationService;
import de.grosjohann.android.feuerwehr.service.IntentServiceFacade;
import de.grosjohann.android.feuerwehr.util.JSON2ObjectUtil;

public class CalendarActivity
  extends Activity {

  private static final String NAME = "CalendarActivity";

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.calendar);

    ListView lv = (ListView) findViewById(R.id.lvCalendarItems);
    BaseAdapter baseAdapter = new CalendarListAdapter(this, R.layout.calendar_item);
    lv.setAdapter(baseAdapter);

    lv.setTextFilterEnabled(false);
    lv.setDividerHeight(10);

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
      Log.i(NAME, "started - has extras");
      updateCalendarList((String) extras.get(CalendarNotificationService.CALENDAR_DATA));
    }

    CalendarDisplayService.updateUIListener = new IUpdateUIListener() {
      @Override
      public void updateUI(final String newData) {
        runOnUiThread(new Runnable() {
          @Override
          public void run() {
            updateCalendarList(newData);
          }
        });
      }

      @Override
      public void showInfo(String info) {
        // nothing to do
      }

      @Override
      public void showWarning(String warning) {
        // nothing to do
      }

      @Override
      public void showError(final String error) {
        runOnUiThread(new Runnable() {
          @Override
          public void run() {
            Panel panel = new Panel(getApplicationContext(), CalendarActivity.this);
            panel.showError(error);
          }
        });

      }

    };
  }

  @Override
  protected void onResume() {
    super.onResume();
    Notify.getInstance().clear(this);

    IntentServiceFacade.startOnce(this, CalendarDisplayService.class);
  }

  /*
   * Update of WeatherData.
   */
  private void updateCalendarList(String newData) {
    try {

      /*
       * Liste der Calendar-Items
       */

      List<CalendarItem> list = JSON2ObjectUtil.getCalendarItems(newData);

      ListView lv = (ListView) findViewById(R.id.lvCalendarItems);
      ((CalendarListAdapter) lv.getAdapter()).addAll(list);

    } catch (JSONException ex) {
      Log.e(NAME, ex.getMessage(), ex);

      Panel panel = new Panel(getApplicationContext(), this);
      panel.showError(R.string.errReceiveCalendarData);
    }
  }
}
